package fr.afcepf.atod21.expedition.daoImpl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import fr.afcepf.atod21.expedition.dao.BasicDaoImpl;
import fr.afcepf.atod21.expedition.entity.Commande;

public class CommandeDaoJpa extends BasicDaoImpl<Commande> {

	@Override
	public void persist(Commande anEntity) {
		EntityManager em = Persistence.createEntityManagerFactory("Commande").createEntityManager();
		em.getTransaction().begin();
		em.persist(anEntity);
		em.getTransaction().commit();
	}

	@Override
	public void remove(Commande anEntity) {
		EntityManager em = Persistence.createEntityManagerFactory("Commande").createEntityManager();
		em.getTransaction().begin();
		em.remove(anEntity);
		em.getTransaction().commit();
	}

	@Override
	public Commande find(int id) {
		EntityManager em = Persistence.createEntityManagerFactory("Commande").createEntityManager();		
		return em.find(Commande.class, id);		
	}

	@Override
	public Commande update(Commande anEntity) {
		EntityManager em = Persistence.createEntityManagerFactory("Commande").createEntityManager();		
		return em.merge(anEntity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Commande> findAll() {
		EntityManager em = Persistence.createEntityManagerFactory("Commande").createEntityManager();		
		return em.createQuery("select c from Commande c").getResultList();
	}
}
